//
//  GetWebContentViewController.m
//  GetWebContent
//

#import "GetWebContentViewController.h"

@implementation GetWebContentViewController

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Error"
													  message:[error localizedDescription]
													 delegate:nil
											cancelButtonTitle:@"OK"
											otherButtonTitles:nil];
	[myAlert show];
	[myAlert release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSString *quotes = [[NSString alloc]
						initWithBytes:[receivedData bytes]
						length:[receivedData length]
						encoding:NSASCIIStringEncoding];
	resultsView.text = quotes;
	[quotes release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSLog(@"started");
	
	CGRect resultsFrame = CGRectMake(10,10,300,100);
	resultsView = [[UITextView alloc] initWithFrame:resultsFrame];
	resultsView.font = [UIFont systemFontOfSize:14.0];
	[self.view addSubview:resultsView];
	
	NSString *symbol = @"AAPL,GOOG,MSFT,YHOO,PALM";
	NSString *urlString = [NSString
						   stringWithFormat:@"http://finance.yahoo.com/d/quotes.csv?f=no&s=%@",
						   symbol];
	NSURL *url = [NSURL URLWithString:urlString];

	receivedData = [[NSMutableData alloc] initWithData:nil];
	NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];
	NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
	[req release];
	[conn release];
	
	NSLog(@"done");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
	[resultsView release];
	[receivedData release];
    [super dealloc];
}

@end
